package com.huawei.petapp2.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.MineCenterActivity;
import com.huawei.petapp2.activity.SettingActivity;
import com.huawei.petapp2.utils.PreferenceHelper;
import com.huawei.petapp2.view.CircleImage;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MineFragment2 extends BaseFragment implements View.OnClickListener{
    RelativeLayout mineInfo;
    RelativeLayout mineCheck;
    RelativeLayout mineDingdan;
    CircleImage mineImg;
    TextView mineName;
    String username;
    String imgurl;
    Float balance;
    Integer userid;
    ImageView mine_setting;

    Bitmap bitmap;
    private PreferenceHelper preferenceHelper;
    private View view;


    @Override
    public void initView(View mView) {


        mineInfo=mView.findViewById(R.id.mine_info);
        mineCheck=mView.findViewById(R.id.mine_check);
        mineDingdan= mView.findViewById(R.id.mine_dingdan);
        mineName=mView.findViewById(R.id.mine_name);
        mineImg= (CircleImage) mView.findViewById(R.id.mine_img);
        mine_setting=mView.findViewById(R.id.mine_setting);
        username= PreferenceHelper.getString("username",getContext());
        imgurl=PreferenceHelper.getString("img",getContext());
        balance=PreferenceHelper.getFloat("balance",getContext());
        userid=PreferenceHelper.getInt("userid",getContext());
        mineName.setText(username);

        Glide.with(mView)
                .load(imgurl)
                .dontAnimate()
                .into(mineImg);
        System.out.println(username);
        setSetting(mView);
        mine_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), MineCenterActivity.class);
                getContext().startActivity(intent);
              //  System.out.println("点击了按钮");
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_mine;
    }

    /**
     * 根据URL获取Bitmap
     * */
    public Bitmap getHttpBitmap(String url){
        Bitmap bitmap=null;
        URL myUrl;

        try {
            myUrl=new URL(url);
            HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
            conn.setConnectTimeout(5000);
            conn.connect();
            InputStream is=conn.getInputStream();
            bitmap=BitmapFactory.decodeStream(is);
            //把bitmap转成圆形
            bitmap=toRoundBitmap(bitmap);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //返回圆形bitmap
        return bitmap;
    }


    /**
     * 把bitmap转成圆形
     * */
    public Bitmap toRoundBitmap(Bitmap bitmap){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int r=0;
        //取最短边做边长
        if(width<height){
            r=width;
        }else{
            r=height;
        }
        //构建一个bitmap
        Bitmap backgroundBm=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas=new Canvas(backgroundBm);
        Paint p=new Paint();
        //设置边缘光滑，去掉锯齿
        p.setAntiAlias(true);
        RectF rect=new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //<span style="white-space:pre">	</span>//且都等于r/2时，画出来的圆角矩形就是圆形
                canvas.drawRoundRect(rect, r/2, r/2, p);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, p);
        return backgroundBm;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_setting:
                Intent intent = new Intent(getActivity(), MineCenterActivity.class);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

       ImageView mineSetting = (ImageView) getActivity().findViewById(R.id.mine_setting);

        mineSetting.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(getActivity(), MineCenterActivity.class);
                getContext().startActivity(intent);
                System.out.println("点击了按钮");
                Toast.makeText(getActivity(), "success2", 0).show();
            }
        });
    }


    class MyTask extends AsyncTask<String, String, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String url=arg0[0];
            Bitmap bm=getHttpBitmap(url);
            return bm;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            mineImg.setImageBitmap(result);
        }
    }

    private class ButtonListener implements View.OnClickListener {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.mine_setting:
                    Intent intent = new Intent(getActivity(), MineCenterActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    }

    public void setSetting(View view) {
        this.view = view;
        ImageView  setting = view.findViewById(R.id.mine_setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
    }

}
